<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
	//个人账户
	public function code(){
			$Verify = new \Think\Verify();
			$Verify->fontSize =  20;    // 大小  
			$Verify->length   =    4;    // 个数   
			$Verify->useNoise =   false; // 有无点啥的	
			$Verify->codeSet = '0123456789'; //设置为纯数字	
			$Verify->entry();
	}
	
	public function index(){
		$mod = M('system');
		$system = $mod -> where('sid = "%s" ','1')->find();
		$this -> assign('system',$system);
		if($system['is_close']==1){
			$this -> redirect('home/login/guanbi');
		}else{
				 $this->display('');
		}
		
	}
	function guanbi(){
		$mod = M('system');
		$system = $mod -> where('sid = "%s" ','1')->find();
		$this -> assign('system',$system);
		$this -> display('');
	}
	/*
		2代表验证码ajax返回的错误
		22代表用户名ajax返回的错误
		222代表密码ajax返回的错误
	*/ 
    public function chulidenglu(){
    	//获取验证码

    	$Verify = new \Think\Verify();
		$res = $Verify->check(I('post.code'));
			if(!empty(I('post.'))){
				if($res){
					$mod = M('users');
					$user = $mod->where('user_name="'.I('post.username').'"')->find();
					if($user>0){
						if(empty($user['ec_salt'])){
								$password = md5(I('post.password'));
								if($user['password']==$password){
									echo '1';								
								}else{
									echo '222';
								}
							}else{
								$password = md5(md5(I('post.password')).$user['ec_salt']);
								if($user['password']==$password){
									echo '1';								
								}else{
									echo '222';
								}
							}
					}else{
						echo '22';		
					}       
		    	}else{
		    		echo '2';
		    	
		    	}
			}else{
				$this -> error('抱歉您还没登录',U('home/login/index'));
			}
			

	}

	//处理登录信息
	public function dologin(){
		//判断用户是否被冻结
		$user = M('users') -> where('user_name = "%s" ',I('post.username'))->find();
		$dongjie = $user['is_jh'];
		$jihuo = $user['is_fh'];
		// dump($user);die;
		if($jihuo==0){
			if($dongjie==0){
								//后台登录
							if(!empty(I('get.ad_us_id'))){
					    		$_SESSION['users'] = M('users') -> where('user_id = "%d"',I('get.ad_us_id'))->find();
					    		$this ->success('恭喜登陆成功',U('home/index/index'));
					    	}else{
					    		$mod = M('users');
							$user = $mod->where('user_name = "%s" ',I('post.username'))->find();
							$_SESSION['users'] = $user;
							$this ->success('恭喜登陆成功',U('home/index/index'));
				    		}
						}else{
							$this -> error('账号被冻结,请联系领导人',U('home/login/index'));
						}

		}else{
			$this -> error('账号未激活,请联系报单中心',U('home/login/index'));
		}
			
		
		
	}

   //消除session
	public function outlogin(){
		session('[destroy]');
		if(!$_SESSION){
			$this -> success('退出成功',U('home/login/index'));
		}else{
			$this -> error('退出失败',U('home/person/index'));
		}
	}
	//忘记密码
	public function forgetpassword(){
	
		$this -> display('');
	}
	//验证用户名和身份证
	public function phoneAjax(){
		if(!empty(I('post.'))){
			$mod = M('users');
			$data['user_name'] = array('eq',I('post.user_name'));
			$data['cardno'] = array('eq',I('post.cardno'));
			$res = $mod -> where($data) ->select();		
			if($res > 0){
					$_SESSION['phone']['code'] = rand('111111','999999');
					$_SESSION['phone']['mobile'] =  $res[0]['mobile_phone'];
					$_SESSION['phone']['content']=",您的短信验证码为".$_SESSION['phone']['code']."，仅用于修改密码、卖出现金积分操作，短信验证码的有效时间为5分钟，使用后即作废。";
				
				$this -> send_msg();
			}else{
				echo  '2';
			}
		}else{
			$this -> error('抱歉您还没登录',U('home/login/index'));
		}		
		

	}
	//处理修改密码
	public function editPassword(){
		$mod = M('users');
		
		if($_SESSOION['phone']['code'] == I('post.code')){
			$data['user_name'] = array('eq',I('post.user_name'));			
			$user = $mod -> where($data)->find();
			if(!empty($user['ec_salt'])){
				$user['password'] = md5(md5(I('post.newpassword')).$user['ec_salt']);
			}else{
				$user['ec_salt'] = rand('1111','9999');
				$user['password'] = md5(md5(I('post.newpassword')).$user['ec_salt']);
			}			
			
			$res = $mod -> where($data)->save($user);			
			if($res>0){	
				session('phone',null);	
				echo '1';
			}else{				
				echo '22';
			}
		}else{
			echo '222';
		}
	}



	public function send_msg(){
				    // //$msgusrl="http://222.73.117.156/msg/HttpBatchSendSM?account=vip_lxgj66&pswd=O3l7Nhs8k&mobile=".$msg_mobile."&msg=".$msg_content."&needstatus=true";
				    // $url ="http://api.sms.cn/msg";
				   
				    // $postFields=array (
								//           'un' => 'dong354698601',
								//           'pw' => 'dong6395175346',
								//           'msg' => $msg_content,
								//           'phone' => $msg_mobile,
								//           'rd' => 1
				    //                  );
				    // curlPost($url,$postFields);
		//			
					//接口账号
					$uid = 'dong354698601';

					//登录密码
					$pwd = 'dong6395175346';

					/**
					* 实例化接口
					* 
					* @param string $uid 接口账号
					* @param string $pwd 接口密码					*/
				
					$api = new \Org\Util\Smsapi($uid,$pwd);

					$mobile = $_SESSION['phone']['mobile'];

					//短信内容参数
					$contentParam = array(
						'code'		=> $_SESSION['phone']['code'],
						'username'	=> $_SESSION['phone']['content']
						);

					//变量模板ID
					$template = '397816';

					//发送变量模板短信
					$result = $api->send($mobile,$contentParam,$template);

					if($result['stat']=='100')
					{
						echo '发送成功';
					}
					else
					{
						echo '发送失败:'.$result['stat'].'('.$result['message'].')';
					}

		}

 


}